package com.bjb.vr.server.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.imps.CuratorFrameworkState;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description:
 * @author: HuJingBo
 * @time: 2022/3/16 14:17
 */
@Slf4j
@Configuration
@ConditionalOnProperty(value = "zookeeper.monitor.enable", matchIfMissing = false)
public class ZkClientConfig {

    @Value("${zookeeper.address}")
    private String connectString;


    @Bean(name = "zkClient")
    public CuratorFramework zkClient() {
        // 连接时间和重试次数
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        client.start();
        CuratorFrameworkState state = client.getState();
        log.info("【初始化ZooKeeper连接状态....】= {}", state);
        return client;
    }
}


